package cn.jdy.payment.web;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.payment.entity.Orders;
import cn.jdy.payment.service.OrderService;

/**
 * Created by Heavens on 2014/5/31.
 */
@Controller
@RequestMapping("/order")
public class OrderCtrl extends BaseCtrl {

	@Autowired
	private OrderService orderService;

	/**
	 * 查询订单
	 */
	@RequestMapping("/queryOrders")
	public ModelAndView queryOrders(Orders order, String fromDate, String toDate) {
		ModelAndView mv = new ModelAndView("/my/myOrder.jsp");
		try {
			//设置分页信息
			if(order.getiDisplayLength() == 0) order.setiDisplayLength(5);                //默认长度5
			if(order.getPageNo()<=0) order.setPageNo(1);                                  //默认页数1
			order.setiDisplayStart((order.getPageNo() - 1) * order.getiDisplayLength());  //显示起始索引

			order.setUserId(getMember(false).getId());

			mv.addObject("dataList", orderService.queryOrders(order, fromDate, toDate));
			mv.addObject("total", orderService.count(order, fromDate, toDate));
			mv.addObject("pageNo", order.getPageNo());
			mv.addObject("fromDate", fromDate);
			mv.addObject("toDate", toDate);
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "queryOrders", e);
		}
		return mv;
	}

	/**
	 * 根据时间段下载我的订单信息
	 *
	 * @return
	 */
	@RequestMapping("/downExcel")
	public void downExcel(Orders order, String fromDate, String toDate, HttpServletResponse response) {
		order.setUserId(getMember(false).getId());
		List<Orders> list = orderService.queryOrders(order, fromDate, toDate);
//		for(Orders o : list) {
//			String s;
//			switch(o.getStatus()) {
//				case "1": s = "成功"; break;
//				case "2": s = "失败"; break;
//				case "3": s = "用户撤销"; break;
//				case "4": s = "未支付"; break;
//				default: s = ""; break;
//			}
//			o.setStatus(s);
//		}
		String[] titles = new String[]{"购买时间", "姓名", "联系方式", "代理地区", "店面面积", "投入金额", "代理详情", "品类"};
		String[][] fieldNames = new String[][]{{"orderDate"}, {"business.userName"}, {"business.telephone"},
				{"business.province", "business.city", "business.county", "business.address"},
				{"business.acreage"}, {"business.inmoney"}, {"business.agentDetails"}, {"business.bPinlei.value0"}};
		super.downExcel(list, titles, fieldNames, "我的订单", response);
	}
}
